package com.sinbad.produce.utils;

import com.sinbad.produce.vo.Content;
import com.sinbad.produce.vo.KnowParticular;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author sinbad
 * date 2021/8/13 11:09
 */
@Component
public class HtmlParseUtils {

    /**
     * 爬取数据
     */
    public List<Content> listGoods(String keyWork) throws IOException {
        String url = "https://search.jd.com/Search?keyword=" + keyWork;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements li = element.getElementsByTag("li");
        ArrayList<Content> list = new ArrayList<>();
        for (Element elements : li) {
            String img = elements.getElementsByTag("img").eq(0).attr("src");
            String price = elements.getElementsByClass("p-price").eq(0).text();
            String title = elements.getElementsByClass("p-name").eq(0).text();
            Content content = new Content();
            content.setImg(img);
            content.setPrices(price);
            content.setTitle(title);
            list.add(content);
        }
        return list;
    }

    /**
     * 爬取 http://lxjk.people.cn/GB/404329/414621/
     *
     * @return
     */
    public List<KnowParticular> getKnowParticular() throws IOException {
        ArrayList<KnowParticular> list = new ArrayList<>();
        String KNOWLEDGEID = "XHXT0001";
        String KNOWLEDGEDESC = "通过有效的人工通气迅速纠正缺氧是淹溺现场急救的关键";
        String url = "http://lxjk.people.cn/GB/404329/414621/index2.html";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements elements = document.getElementsByClass("clearfix p2j_listBox w1000").tagName("ul").tagName("li");
        for (Element li : elements) {
            Elements target = li.getElementsByAttribute("target");
            int size = target.size();
            for (int i = 0; i < size; i++) {
                String titleVal = target.eq(i).text();
                String releaseTime = li.getElementsByTag("em").eq(i).text().substring(1,17);
                String knowledgeContent = "根据世界卫生组织（WHO）的统计，全球每年约有37万人死于溺水，意味着每天每小时有40人因溺水而失去生命。每年夏天是发生溺水最严重的季节，在青少年意外伤害致死的事故中，溺水事故是头号杀手。根据临床研究，如果淹没时间少于10min，那么淹溺者预后良好的可能性非常高，而如果淹没时间超过25min，则预后极差。" +
                        "溺水，又称淹溺，溺水时口、鼻、咽喉部等气道入口处形成一道液/气界面，从而阻止人的呼吸，通过有效的人工通气迅速纠正缺氧是淹溺现场急救的关键。而第一目击者和专业急救人员快速有效的抢救是可以改变结局的。";
                KnowParticular knowParticular = new KnowParticular();
                knowParticular.setBrowseCount(new Random().nextInt(900) + 100);
                knowParticular.setKnowledgeId(KNOWLEDGEID);
                knowParticular.setKnowledgeDesc(KNOWLEDGEDESC);
                knowParticular.setKnowledgeTitle(titleVal);
                knowParticular.setKnowledgeContent(knowledgeContent);
                knowParticular.setReleaseTime(releaseTime);
                knowParticular.setState(1);
                list.add(knowParticular);
            }

        }
        return list;
    }
}
